import Vue from 'vue'
// import routes from './routes'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource';
Vue.use(VueResource);
require("font-awesome-webpack");
import $ from 'jquery';
// require('bootstrap-loader');
import 'simple-line-icons/css/simple-line-icons.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap/dist/js/bootstrap.min.js';
import './assets/components.min.css';
import './assets/plugins.min.css';
import './assets/layout.min.css';
import './assets/light2.min.css';
import './assets/huamotec.style.1.0.css';
import './assets/style.min.css';
import './assets/layer.css';
import './assets/select2.css';
import './assets/select2-bootstrap.min.css';

require('./vendor/macarons.js');
import App from './vendor/app.min.js';
require('./vendor/layout.js');
require('./vendor/layer.js');
require('./vendor/select2.full.js');
require('./vendor/components-select2.js');

var cookie = require('cookie');
var moment = require('moment');
//页面
import about from './pages/About.vue';
import home from './pages/Home.vue';
import homePage from './pages/HomePage.vue';
import login from './pages/login.vue';
import none from './pages/404.vue';
import patientList from './pages/patientList.vue';
import test1 from './pages/test1.vue';
import test2 from './pages/test2.vue';
import test3 from './pages/test3.vue';
import test4 from './pages/test4.vue';
import test5 from './pages/test5.vue';


// 组件
import navBar from './components/navBar.vue';
import top from './components/top.vue';
Vue.use(VueRouter);
// var App = Vue.extend({});
const routes=[
  {path:"/Login",component:login,name:"Login"},
  {
      path:"/",
      component:homePage,
      redirect: '/home',
      children:[{path:"home",component:patientList,name:'home'}]
  },{
      path:"/",
      component:homePage,
      redirect: '/About',
      children:[{path:"About",component:about,name:'About'}]
  },
  {
      path:"/DialysisCenterManagement",
      component:homePage,
      redirect: '/DialysisCenterManagement/test',
      name:"DialysisCenterManagement",
      children:[
          {path:"test",component:test1,name:"test"},
          {path:'test2',component:test2,name:"test2"},
          {
              path:"personRegister",
              name:"personRegister",
              redirect: '/DialysisCenterManagement/personRegister/test3',
              children:[
                  {path:"test3",component:test3,name:"test3"},
                  {path:"test4",component:test4,name:"test4"},
              ]
          }
      ]
  },
  {
      path:"/PhysicalExamination",
      component:homePage,
      redirect: '/PhysicalExamination/test5',
      name:"PhysicalExamination",
      children:[
          {path:"test5",component:test5,name:"test5"}
      ]
  },
  {
    path:"/404",component:none,name:"404"
  }
]
  
var router = new VueRouter({
  routes,
  mode: 'hash',
  linkActiveClass: ['active','open'], // 这是链接激活时的class
});
//后台接口请求拦截，添加token和userId
Vue.http.interceptors.push(function(request,next){
    var user = JSON.parse(sessionStorage.getItem('user'));
    var token = (window.sessionStorage.getItem("Token"))?(window.sessionStorage.getItem("Token")):"";
    var Uid = user?((user.Id)?(user.Id):""):null;
    if(token && Uid){
        // request.headers.set('Token', token);
        var isUid =request.url.indexOf('UserId=');
        var is = request.url.indexOf('?');
        if(is>-1){
            //存在值
            if(isUid>-1){
                //若已经存在uid
                if(method(request.url,token)==0){
                    //不存在token
                    request.url = request.url +'&Token='+token;
                }else{
                    request.url = method(request.url,token);
                }
            }else{
                //若不存在uid
                if(method(request.url,token)==0){
                    //不存在token
                    request.url = request.url +'&UserId='+Uid+'&Token='+token;
                }else{
                    request.url = method(request.url,token);
                }
            }
        }else{
            request.url = request.url +'?Token='+token+'&UserId='+Uid;
        }
    }
    next(function(response) {
        if(response.body.Code == 'HM700' || response.body.Code == 'HM600'){
            sessionStorage.setItem("TokenCode", response.body.Code);
            //页面跳转
            // top.location.href="../login/Login.html";
            router.push('/Login');
        }
    });
});
function method(ul,token){
    var isToken = ul.indexOf('Token=');
    var f;
    if(isToken>-1){
        var b = ul.indexOf('Token=');
        var c = ul.substring(b+6); 
        var d = c.indexOf('&');
        if(d>-1){
            f=ul.substring(0,b)+'Token='+token+c.substring(d); 
        }else{
            f=ul.substring(0,b)+'Token='+token; 
        }
    }else{
        f=0;
    }
    return f;
}
router.beforeEach((to, from, next) => {
  if (to.path == '/Login' ) {
    sessionStorage.removeItem('user');
  }
  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path != '/Login') {
    next({ path: '/Login' })
  }else if(user && to.path=='/'){
    //说明已经登录过
    next({path:from.path});
  }else {
    next();
  }
});
//挂载到#app
// router.start(App,"#app");
const Apps = new Vue({
  router,
  components:{
    navBar,
    top   
  },
  mounted:function(){
    // this.get();
  },
  methods:{
     get:function(){
          Vue.http.get('../../test.json'
          ).then(function(result) {
              console.log(result.data);
          });
      },
  }
}).$mount('#app');
